package com.powerhua.core.xml;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;

public class SystemConfigLocator {

    static Logger logger = Logger.getLogger(SystemConfigLocator.class);
    public static final String CONFIG_FILE = "config-system.xml";

    /**
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
	public static List getContentFileSavePath() {
        List<String> list = new ArrayList<String>();

        // get real path from xml file
        Element realPathElement = getElement("realPath");
        String realPathAvailbleValue = realPathElement.attribute("available").getValue();
        if ("true".equals(realPathAvailbleValue)) {
            list = getListValue(realPathElement);
        } else {
            // default get path
            Element pathElement = getElement("path");
            list = getListValue(pathElement);
        }
        return list;
    }

    /**
     * 
     * @return
     */
    public static String getWebpageTemplateFilePath() {
        Element webpageTemplateFilePathElement = getElement("WebpageTemplateFilePath");
        String returnString = webpageTemplateFilePathElement.getTextTrim();
        return returnString;
    }

    /**
     * 
     * @param strId
     * @return
     */
    private static Element getElement(String strId) {
        Document doc = XmlUtil.getDocument(CONFIG_FILE);
        Element root = doc.getRootElement();
        Element element = root.element(strId);

        return element;
    }

    /**
     * 
     * @param element
     * @return
     */
    @SuppressWarnings("unchecked")
	private static List<String> getListValue(Element element) {
        List<String> returnList = new ArrayList<String>();
        List<Element> values = element.element("values").elements();
        for (int i = 0; i < values.size(); i++) {
            Element value = (Element) values.get(i);
            returnList.add(value.getTextTrim()); 
        }
        return returnList;
    }

    /**
     * For test
     * @param args
     */
    @SuppressWarnings("unchecked")
	public static void main(String[] args) {
        List list = getContentFileSavePath();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        System.out.println(getWebpageTemplateFilePath());

    }
}
